package redis

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"strconv"
)

const Title = "Goods_Stock:Goods_Stock_"

func AddGoodsStock(goodsId, goodsStock int64) error {
	for i := 0; i < int(goodsStock); i++ {
		err = redisClient.RPush(Title+strconv.Itoa(int(goodsId)), goodsStock).Err()
		if err != nil {
			logx.Error("添加虚拟库存失败", err)
		}
	}
	return err
}

// 获取商品虚拟库存
func GetGoodsStock(goodsId int64) (int64, error) {
	stock, _ := redisClient.LLen(Title + strconv.Itoa(int(goodsId))).Result()

	return stock, nil
}

// 移除商品虚拟库存
func RemoveGoodStock(goodsId, goodsStock int64) error {
	for i := 0; i < int(goodsStock); i++ {
		err = redisClient.LPop(Title + strconv.Itoa(int(goodsId))).Err()
		if err != nil {
			fmt.Println("移除商品虚拟库存失败")
		}
	}
	return nil
}
